# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=tcl
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=8.6.10
pkgrel=1
pkgdesc="The Tcl scripting language (mingw-w64)"
arch=('any')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-zlib")
#conflicts=(${MINGW_PACKAGE_PREFIX}-sqlite-analyzer)
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs') # '!strip' 'debug')
license=("custom")
url="https://tcl.sourceforge.io/"
source=("https://downloads.sourceforge.net/sourceforge/tcl/tcl${pkgver}-src.tar.gz"
        001-fix-relocation.patch
        002-fix-forbidden-colon-in-paths.mingw.patch
        004-use-system-zlib.mingw.patch
        005-no-xc.mingw.patch
        006-proper-implib-name.mingw.patch
        007-install.mingw.patch
        008-tcl-8.5.14-hidden.patch
        009-fix-using-gnu-print.patch)

sha256sums=('5196dbf6638e3df8d5c87b5815c8c2b758496eb6f0e41446596c9a4e638d87ed'
            'cfcf9b3816f8bb063b514ac7f63a5ba73108f27e16fdf8e8312dc5f0683083f6'
            '70bf0d8e84985f4e8ee63447ad37d5e50376eaf35ace51112761cacbbd596c4c'
            '2d7581ad118c01afcebd762051eb74fe31511e8b40505554068f126bcd8d5d9f'
            '2b0f41f6704aa964dbfafa0a65dd5ce0ab97e82ff5cbbe2a95a2e8d644cc5550'
            '9ffbba1a185fc3425d1398361472df3ca2a488ed9101970dc3325d512f0d6e19'
            'f1833c3164229b017417d2ab2ce4cb066252fc1ad256de2313f0239481c7cc37'
            '3ec2702efb1be6873d6ffd2ffb357637588f835f8817ae65cf0373020fcc7359'
            'e49a314ff0262e487f15fa1cb6253e22e25e8b18dad0b057d600e833efa947a7')

prepare() {
  cd "${srcdir}/tcl${pkgver}"
  patch -Np1 -i "${srcdir}/001-fix-relocation.patch"
  patch -Np1 -i "${srcdir}/002-fix-forbidden-colon-in-paths.mingw.patch"
  patch -Np1 -i "${srcdir}/004-use-system-zlib.mingw.patch"
  patch -Np1 -i "${srcdir}/005-no-xc.mingw.patch"
  patch -Np1 -i "${srcdir}/006-proper-implib-name.mingw.patch"
  patch -Np1 -i "${srcdir}/007-install.mingw.patch"
  patch -Np1 -i "${srcdir}/008-tcl-8.5.14-hidden.patch"
  patch -Np1 -i "${srcdir}/009-fix-using-gnu-print.patch"

  # Using the static libgcc library is problematic when sharing
  # resources across dynamic link libraries, so we must use
  # libgcc*.dll everywhere:
  echo "Removing -static-libgcc from configure files"
  find "${srcdir}/tcl${pkgver}" -type f \( -name "tcl.m4" -o -name "configure*" \) -print0 | xargs -0 sed -i 's/-static-libgcc//g'

  cd win && autoreconf -fi
}

build() {
  local extra_config=
  if check_option "debug" "y"; then
    extra_config="--enable-symbols"
  fi
  local enable64bit=
  [[ "${MINGW_CHOST}" = "x86_64-w64-mingw32" ]] && enable64bit='--enable-64bit'

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST}
  cd "${srcdir}/build-${MINGW_CHOST}"
  ${srcdir}/tcl${pkgver}/win/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-threads \
    ${extra_config} \
    ${enable64bit}

  make
}

package() {
  local debug_suffix=
  if check_option "debug" "y"; then
    debug_suffix="g"
  fi
  cd "${srcdir}/build-${MINGW_CHOST}"
  cp tclsh86${debug_suffix}.exe tclsh.exe
  #PATH=.:$PATH
  make install INSTALL_ROOT="$pkgdir"

  cp "${pkgdir}${MINGW_PREFIX}/bin/tclsh86${debug_suffix}.exe" "${pkgdir}${MINGW_PREFIX}/bin/tclsh.exe"
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
  find "${pkgdir}${MINGW_PREFIX}" -name '*.sh' -o -name '*.dll' -o -name '*.exe' | xargs -rtl1 chmod 755

  local _libver=${pkgver%.*}
  _libver=${_libver//./}

  local _odbc_ver=1.1.1
  local _itcl_ver=4.2.0
  sed \
    -e "s|^\(TCL_BUILD_LIB_SPEC\)='.*|\1='-Wl,${MINGW_PREFIX}/lib/libtcl${_libver}.dll.a'|" \
    -e "s|^\(TCL_SRC_DIR\)='.*'|\1='${MINGW_PREFIX}/include/tcl${pkgver%.*}/tcl-private'|" \
    -e "s|^\(TCL_BUILD_STUB_LIB_SPEC\)='.*|\1='-Wl,${MINGW_PREFIX}/lib/libtclstub${_libver}.a'|" \
    -e "s|^\(TCL_BUILD_STUB_LIB_PATH\)='.*|\1='${MINGW_PREFIX}/lib'|" \
    -e "s|^\(TCL_STUB_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib -ltclstub${_libver}'|" \
    -e "s|^\(TCL_INCLUDE_SPEC\)='.*|\1='-I${MINGW_PREFIX}/include/tcl${pkgver%.*}'|" \
    -i "${pkgdir}${MINGW_PREFIX}/lib/tclConfig.sh"

  sed \
    -e "s|^\(tdbc_BUILD_STUB_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib/tdbc${_odbc_ver} -ltdbcstub${_odbc_ver//./}'|" \
    -e "s|^\(TDBC_BUILD_STUB_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib/tdbc${_odbc_ver} -ltdbcstub${_odbc_ver//./}'|" \
    -e "s|^\(tdbc_BUILD_STUB_LIB_PATH\)='.*|\1='${MINGW_PREFIX}/lib/tdbc${_odbc_ver}/libtdbcstub${_odbc_ver//./}.a'|" \
    -e "s|^\(TDBC_BUILD_STUB_LIB_PATH\)='.*|\1='${MINGW_PREFIX}/lib/tdbc${_odbc_ver}/libtdbcstub${_odbc_ver//./}.a'|" \
    -e "s|^\(tdbc_INCLUDE_SPEC\)='.*|\1='${MINGW_PREFIX}/lib/tdbc${_odbc_ver}/libtdbcstub${_odbc_ver//./}.a'|" \
    -e "s|^\(tdbc_INCLUDE_SPEC\)='.*|\1='${MINGW_PREFIX}/lib/tdbc${_odbc_ver}/libtdbcstub${_odbc_ver//./}.a'|" \
    -i "${pkgdir}${MINGW_PREFIX}/lib/tdbc${_odbc_ver}/tdbcConfig.sh"

  sed \
    -e "s|^\(itcl_BUILD_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib/itcl${_itcl_ver} -litcl${_itcl_ver//./}'|" \
    -e "s|^\(ITCL_BUILD_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib/itcl${_itcl_ver} -litcl${_itcl_ver//./}'|" \
    -e "s|^\(itcl_BUILD_STUB_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib/itcl${_itcl_ver} -litclstub${_itcl_ver//./}'|" \
    -e "s|^\(ITCL_BUILD_STUB_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib/itcl${_itcl_ver} -litclstub${_itcl_ver//./}'|" \
    -e "s|^\(itcl_BUILD_STUB_LIB_PATH\)='.*|\1='${MINGW_PREFIX}/lib/itcl${_itcl_ver}/libitclstub${_itcl_ver//./}.a'|" \
    -e "s|^\(ITCL_BUILD_STUB_LIB_PATH\)='.*|\1='${MINGW_PREFIX}/lib/itcl${_itcl_ver}/libitclstub${_itcl_ver//./}.a'|" \
    -i "${pkgdir}${MINGW_PREFIX}/lib/itcl${_itcl_ver}/itclConfig.sh"

  ln -s "${pkgdir}${MINGW_PREFIX}/lib/libtcl86${debug_suffix}.dll.a" "${pkgdir}${MINGW_PREFIX}/lib/libtcl.dll.a"
  ln -s "${pkgdir}${MINGW_PREFIX}/lib/tclConfig.sh" "${pkgdir}${MINGW_PREFIX}/lib/tcl${pkgver%.*.*}/tclConfig.sh"

  # Install private headers
  mkdir -p "${pkgdir}${MINGW_PREFIX}/include/tcl${pkgver%.*}/tcl-private/"{generic,win}
  cd ${srcdir}/tcl${pkgver}
  find generic win -name "*.h" -exec cp -p '{}' "${pkgdir}${MINGW_PREFIX}"/include/tcl${pkgver%.*}/tcl-private/'{}' ';'
  ( cd "${pkgdir}${MINGW_PREFIX}/include"
    for header in *.h ; do
      cp -f ${header} ${pkgdir}${MINGW_PREFIX}/include/tcl${pkgver%.*}/tcl-private/generic/
    done
  )
  chmod a-x "${pkgdir}${MINGW_PREFIX}/lib/tcl${pkgver%.*}/encoding/"*.enc
  chmod a-x "${pkgdir}${MINGW_PREFIX}/lib/"*/pkgIndex.tcl

  install -Dm644 ${srcdir}/${_realname}${pkgver}/win/tcl.m4 ${pkgdir}${MINGW_PREFIX}/share/aclocal/tcl${pkgver%.*}.m4
  
  # Resolve conflict with sqlite-analyzer package
  mv ${pkgdir}${MINGW_PREFIX}/bin/sqlite3_analyzer{,.sh}
}
